<?php
/**
 * @Author:         zk
 * @DateTime:       18-3-30 下午3:50
 * @Description:    获取有效的历史保养订单 同步到 order_history_maintain 历史保养订单统计表
 */
$api_service_path = __DIR__ . "/../../../api/";
ob_start();
require_once($api_service_path . "public/index.php");
ob_end_clean();
set_time_limit(0);
//DB::connection()->disableQueryLog();      //关闭日志

//今天统计昨天数据
$start_time = date('Y-m-d', strtotime('-1 day')).' 00:00:00';   //开始时间
$end_time = date('Y-m-d', strtotime('-1 day')).' 23:59:59';     //结束时间
//$start_time = '2016-01-01 00:00:00';   //开始时间
//$end_time = '2018-04-02 23:59:59';     //结束时间
$res = get_history_maintain($start_time, $end_time);

/**
 * 获取有效的历史保养订单 同步到 order_history_maintain 历史保养订单统计表
 * @param date $start_time  开始时间    例如 2018-03-30 00:00:00
 * @param date $end_time    结束时间    例如 2018-03-30 23:59:59
 * @return boolean
 */
function get_history_maintain($start_time, $end_time){
    print_r('.......... '.date('Y-m-d H:i:s').' .......... start running get_history_maintain.php .............');
    
    $order_status = [1117, 1217, 1417, 1018, 1420]; //订单状态
      
    $list = DB::table('orders')
            ->leftJoin('order_product', 'orders.order_id', '=', 'order_product.order_id')
            ->where('orders.pay_time', '>', $start_time)
            ->where('orders.pay_time', '<', $end_time)
            ->whereIn('orders.status', $order_status)
            ->where('order_product.status', 1)
            ->orderBy('orders.pay_time', 'asc')
            ->select('orders.order_id', 'orders.user_id', 'orders.status', 'orders.name', 'orders.phone', 'orders.created_at',  
                    'orders.car_brand_id', 'orders.car_brand_name', 'orders.car_series_id', 'orders.car_series_name', 
                    'orders.car_model_id', 'orders.car_model_name', 'orders.shop_id', 'orders.user_car_id', 
                    'orders.car_no', 'orders.pay_time', 'order_product.product_type', 'order_product.product_id', 
                    'order_product.product_name')
            ->get();
    $insert_arr = [];
    $created_at = date('Y-m-d H:i:s', time());
    $note = '历史保养订单统计';
    if(empty($list)){
        Log::info('时间 '.$start_time.' 至 '.$end_time.' 之间没有保养订单');
	echo '时间 '.$start_time.' 至 '.$end_time.' 之间没有保养订单\n';
        return TRUE;
    }
    foreach ($list as $v) {
        if($v->product_id == 2){    //订单产品 中的product_id为2 基础保养
            $insert_arr[$v->order_id] = [
                'order_id' => $v->order_id,
                'user_id' => $v->user_id,
                'status' => $v->status,
                'name' => $v->name,
                'phone' => $v->phone,
                'user_car_id' => $v->user_car_id,
                'car_no' => $v->car_no,
                'created_time' => $v->created_at,
                'pay_time' => $v->pay_time,
                'car_brand_id' => $v->car_brand_id,
                'car_brand_name' => $v->car_brand_name,
                'car_series_id' => $v->car_series_id,
                'car_series_name' => $v->car_series_name,
                'car_model_id' => $v->car_model_id,
                'car_model_name' => $v->car_model_name,
                'shop_id' => $v->shop_id,
                'product_type' => $v->product_type,
                'product_id' => $v->product_id,
                'product_name' => $v->product_name,
                'created_at' => $created_at,
                'note' => $note,
            ];
        } elseif ($v->product_type == 'activity') {     //order_product_acpackage"订单活动包的产品包"
            $ac_res = DB::table('order_product_acpackage')
                    ->where('order_id', $v->order_id)
                    ->where('package_id', 2)
                    ->select('package_id', 'package_name')
                    ->first();
            if($ac_res){
                $insert_arr[$v->order_id] = [
                    'order_id' => $v->order_id,
                    'user_id' => $v->user_id,
                    'status' => $v->status,
                    'name' => $v->name,
                    'phone' => $v->phone,
                    'user_car_id' => $v->user_car_id,
                    'car_no' => $v->car_no,
                    'created_time' => $v->created_at,
                    'pay_time' => $v->pay_time,
                    'car_brand_id' => $v->car_brand_id,
                    'car_brand_name' => $v->car_brand_name,
                    'car_series_id' => $v->car_series_id,
                    'car_series_name' => $v->car_series_name,
                    'car_model_id' => $v->car_model_id,
                    'car_model_name' => $v->car_model_name,
                    'shop_id' => $v->shop_id,
                    'product_type' => $v->product_type,
                    'product_id' => $ac_res->package_id,        //order_product_acpackage 中的 产品包id
                    'product_name' => $ac_res->package_name,    //order_product_acpackage 中的 产品包名称
                    'created_at' => $created_at,
                    'note' => $note,
                ];
            }
        } elseif ($v->product_type == 'enterprise') {   //order_product_espackage"订单企业包的产品包"
            $es_res = DB::table('order_product_espackage')
                    ->where('order_id', $v->order_id)
                    ->where('package_id', 2)
                    ->select('package_id', 'package_name')
                    ->first();
            if($es_res){
                $insert_arr[$v->order_id] = [
                    'order_id' => $v->order_id,
                    'user_id' => $v->user_id,
                    'status' => $v->status,
                    'name' => $v->name,
                    'phone' => $v->phone,
                    'user_car_id' => $v->user_car_id,
                    'car_no' => $v->car_no,
                    'created_time' => $v->created_at,
                    'pay_time' => $v->pay_time,
                    'car_brand_id' => $v->car_brand_id,
                    'car_brand_name' => $v->car_brand_name,
                    'car_series_id' => $v->car_series_id,
                    'car_series_name' => $v->car_series_name,
                    'car_model_id' => $v->car_model_id,
                    'car_model_name' => $v->car_model_name,
                    'shop_id' => $v->shop_id,
                    'product_type' => $v->product_type,
                    'product_id' => $es_res->package_id,        //order_product_espackage 中的 产品包id
                    'product_name' => $es_res->package_name,    //order_product_espackage 中的 产品包名称
                    'created_at' => $created_at,
                    'note' => $note,
                ];
            }
        }
    }
    if(!empty($insert_arr)){
        $res = DB::table('order_history_maintain')->insert($insert_arr);    //插入历史保养订单统计表
        print_r('.......... '.date('Y-m-d H:i:s').' .......... end running get_history_maintain.php .............');
        if($res){
            Log::info('时间 '.$start_time.' 至 '.$end_time.' 之间的历史保养订单 统计完毕');
            echo '时间 '.$start_time.' 至 '.$end_time.' 之间的历史保养订单 统计完毕\n';
        }else{
            Log::info('时间 '.$start_time.' 至 '.$end_time.' 之间的历史保养订单 统计失败 error');
            echo '时间 '.$start_time.' 至 '.$end_time.' 之间的历史保养订单 统计失败 error\n';
        }    
    } else {
        $res = FALSE;
    }
    
    return $res;
}





